package org.japo.java.matisse.samples;

import java.awt.Color;
import java.awt.Container;

public class CampoTextoCentradoAsistido extends javax.swing.JFrame {

    public CampoTextoCentradoAsistido() {
        // Antes de la construcción del interfaz
        iniciarAnterior();

        // Construcción del interfaz
        initComponents();

        // Después de la construcción del interfaz
        iniciarPosterior();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txfColor = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Campo de texto centrado");
        setResizable(false);

        txfColor.setFont(new java.awt.Font("SansSerif", 0, 24)); // NOI18N
        txfColor.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txfColor.setText("BLANCO");
        txfColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfColorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txfColor, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addComponent(txfColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txfColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfColorActionPerformed
        procesarTexto();
    }//GEN-LAST:event_txfColorActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CampoTextoCentradoAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CampoTextoCentradoAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CampoTextoCentradoAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CampoTextoCentradoAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CampoTextoCentradoAsistido().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField txfColor;
    // End of variables declaration//GEN-END:variables

    private void iniciarAnterior() {
    }

    private void iniciarPosterior() {
    }

    private void procesarTexto() {
        // Obtiene el texto introducido
        String color = txfColor.getText().toUpperCase();

        // Obtiene el panel contenedor
        Container ctnPrincipal = this.getContentPane();

        // Analiza el texto introducido
        switch (color) {
            case "BLANCO":
                ctnPrincipal.setBackground(Color.WHITE);
                break;
            case "ROJO":
                ctnPrincipal.setBackground(Color.RED);
                break;
            case "VERDE":
                ctnPrincipal.setBackground(Color.GREEN);
                break;
            case "AZUL":
                ctnPrincipal.setBackground(Color.BLUE);
                break;
            default:
                ctnPrincipal.setBackground(new Color(240, 240, 240));
        }
    }
}
